<template>
  <view>
    <!-- 顶部搜索区 -->
    <view class="search-box">
      <uni-search-bar placeholder="请输入搜索关键词" cancelButton="none" :focus="true" :radius="18" @input="input">
      </uni-search-bar>
    </view>
    <!-- 搜索建议显示区 -->
    <view class="su-list" v-if="keyWorld.length>0">
      <view v-if="isShow">
        <view class="su-item" v-for="(item,index) in searchResList" :key="index" @click="goToDetail(item)">
          <view class="text">{{item.goods_name}}</view>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
      </view>
      <view class="notfound" v-else>
        <text calss="content">没有找到相关搜索内容</text>
      </view>
    </view>
    <!-- 搜索历史区 -->
    <view class="his-box" v-else>
      <!-- 上部文字区 -->
      <view class="his-content">
        <text>搜索历史</text>
        <uni-icons type="trash" @click="clearHistory"></uni-icons>
      </view>
      <!-- 下部展示区 -->
      <view class="his-list">
        <uni-tag :text="item" v-for="(item,index) in historyList" :key="index" @click="goToGoosList(item)">
        </uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: '',
        keyWorld: '',
        searchResList: [],
        isShow: true,
        historyList: []
      };
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
      // console.log(JSON.parse(uni.getStorageSync('kw') || '[]'))
      console.log(this.historyList)
    },
    methods: {
      // 输入框输入事件
      input(e) {
        clearTimeout(this.timer)
        // 防抖处理
        this.timer = setTimeout(() => {
          this.keyWorld = e.trim()
          // console.log('关键词',this.keyWorld)
          this.getSearchList()
        }, 500)
      },
      // 获取建议内容
      async getSearchList() {
        if (this.keyWorld.length) {
          const {
            data: res
          } = await uni.$http.get('/api/public/v1/goods/qsearch', {
            query: this.keyWorld
          })
          if (res.meta.status !== 200) {
            uni.showMsg('搜索过程发生错误...')
          } else {
            this.searchResList = res.message
            if (this.searchResList.length > 0) {
              this.isShow = true
            } else {
              this.isShow = false
            }
            // 添加搜索历史
            this.historyList.unshift(this.keyWorld)
            // 数组去重
            this.historyList = [...new Set(this.historyList)]
            uni.setStorageSync('kw', JSON.stringify(this.historyList))
          }
          console.log(res)
        } else {
          this.searchResList = []
        }
      },
      // 页面跳转
      goToDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
      // 清空历史
      clearHistory() {
        this.historyList = []
        uni.removeStorageSync('kw')
      },
      // 页面跳转
      goToGoosList(kw) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + kw
        })
      }
    }
  }
</script>

<style lang="scss">
  // 顶部搜索样式
  .search-box {
    position: sticky;
    top: 0;
    z-index: 1;
    background-color: #c00000
  }

  // 搜索建议
  .su-list {
    padding: 0 5px;

    .su-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 10px 0;
      font-size: 13px;
      border-bottom: 1px solid #eee;

      .text {
        white-space: nowrap;
        overflow: hidden;
        // 多余内容用...显示
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }

  // 搜索历史
  .his-content {
    height: 25px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 5px;
    font-size: 13px;
    border-bottom: 1px solid #eee;
  }

  .his-list {
    display: flex;
    margin: 10px 0 0 0;
    flex-wrap: wrap;

    .uni-tag {
      margin: 5px 0 0 5px;
      color: black;
      background-color: #f6f9f8;
      border: 1px solid #eee;
    }
  }

  .notfound {
    display: flex;
    justify-content: center;
    padding: 5px;

    .content {
      font-size: 12px;
    }
  }
</style>
